import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created with IntelliJ IDEA.
 * Description:生产者消费者模型
 * User: 邓大帅
 * Date: 2024-01-25
 * Time: 15:16
 */
public class ThreadDemo3 {
    public static void main(String[] args) {
        BlockingDeque<Integer> blockingDeque = new LinkedBlockingDeque<>();
//        消费者
        Thread thread = new Thread(() -> {
            while (true) {
                try {
                    int value = blockingDeque.take();
                    System.out.println("消费:" + value);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
//        生产者
        Thread thread1 = new Thread(() -> {
            int value = 0;
            while (true) {
                try {
                    System.out.println("生产：" + value);
                    blockingDeque.put(value);
                    value++;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();
    }
}
